<?php

/**
 * Contains a class to display /browse.php.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/** @uses paginate To create pagination links. */
require_once 'function_paginate.php';

/** @uses valueOf For safe handling of arrays. */
require_once 'function_valueof.php';
 
/**
 * A class to display /browse.php.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class PageBrowse extends Page
{
    /**
     * Constructor function.  Calls the parent constructor, sets the title,
     * and sets session variables.
     */
    public function __construct()
    {
        parent::__construct();
        $categoryId = valueOf($_GET, 'categoryid'); 
        if (valueOf($_GET, 'page') != '') {
            $_SESSION['storepage'] = valueOf($_GET, 'page');
        } elseif (valueOf($_SESSION, 'storepage') == '') {
            $_SESSION['storepage'] = 1;
        }
        if ($categoryId != '') {
            $_SESSION['category'] = $this->db->getCategoryById($categoryId);
            if (valueOf($_GET, 'page') == '') {
                $_SESSION['storepage'] = 1;
            }
        }
        parent::setTitle('Browse: ' . $_SESSION['category']->name);
    }

    /**
     * Function to return the HTML to print the pagination links.
     *
     * @access private
     * @return string  The HTML containing the pagination links.
     */
    private function _getPagination()
    {
        $page = $_SESSION['storepage'];
        $total = $this->db->getNumberOfStores();
        return paginate($page, $total, STORES_PER_PAGE);
    }
    
    /**
     * Function to print the list of stores matching the category criteria.
     *
     * @access private
     * @return null
     */
    private function _printStores()
    {
        $pagination = $this->_getPagination();
        $stores     = $this->db->getStoresByCategoryId(
            $_SESSION['category']->id,
            $_SESSION['storepage'],
            $this->user->zip->latitude,
            $this->user->zip->longitude
        );
        $map        = new Map($stores);
        echo $pagination,
           ' <ul class="stores">', "\n";
        $countries = $this->db->getCountries();
        foreach ($stores as $store) {
            $store->country = $countries[$store->country - 1]['name'];
            $store->printListItem();
        }
        echo ' </ul>', "\n",
           $pagination,
           ' <h2>These Stores on a Map</h2>', "\n";
        $map->display();
    }
    
    /**
     * Function to display the page.
     *
     * @access public
     * @return null
     */
    public function display()
    {
        parent::printHeader();
        $this->_printStores();
        parent::printFooter();
    }
}

?>